package com.ruoyi.system.controller;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.LatLngConverter;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.Seq;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.LabLine;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(tags ="通用接口")
@RestController
@RequestMapping("/common")
public class SysCommonController extends BaseController {

    @GetMapping("/getSeqId")
    @ApiOperation("获取项目主键id")
    public AjaxResult getSeqId() {
        return success(Seq.getSeqId());
    }

    @GetMapping("/getNextDays")
    @ApiOperation("获取下一天")
    public AjaxResult getNextDays() {
        return toAjax(DateUtils.dateTime(DateUtils.addDays(new Date(), 1)));
    }

    @GetMapping("/getMonthByNextDays")
    @ApiOperation("获取月份下一天")
    public AjaxResult getMonthNextDays() {
        Date nextDays = DateUtils.addDays(new Date(), 1);
        return toAjax(DateUtils.parseDateToStr(DateUtils.YYYY_MM, nextDays));
    }

    @GetMapping("/getMonthDateRange")
    @ApiOperation("获取当前月份的第1天和最后一天")
    public AjaxResult getMonthDateRange() {
        String firstDay = DateUtils.dateTime(DateUtils.getFirstDayOfMonth(new Date()));
        String endDay = DateUtils.dateTime(DateUtils.getLastDayOfMonth(new Date()));
        String[] range = {firstDay,endDay};
        return success(range);
    }

    /**
     * * 获取当前时间
     *
     * @return
     */
    @GetMapping("/getCurrentTime")
    @ApiOperation("获取当前时间")
    public AjaxResult getCurrentTime() {
        return toAjax(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, new Date()));
    }

    /**
     * * 获取当前日期3个月后的日期
     *
     * @return
     */
    @GetMapping("/getDateToThreeMonth")
    @ApiOperation("获取当前日期3个月后的日期")
    public AjaxResult getDateToThreeMonth(@ApiParam(value = "日期", required = true) @RequestParam("date") Date date) {
        //获取当前日期3个月后的日期
        Date threeMonthDate = DateUtils.addMonths(date, 3);
        return toAjax(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, threeMonthDate));
    }

    /**
     * * 获取当前时间(精确到时分秒)
     *
     * @return
     */
    @GetMapping("/getCurrentDate")
    @ApiOperation("获取当前时间(精确到时分秒)")
    public AjaxResult getCurrentDate() {
        return toAjax(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
    }

    /**
     * * 获取当前登录人
     *
     * @return
     */
    @GetMapping("/getCurrentUser")
    @ApiOperation("获取当前登录人")
    public AjaxResult getCurrentUser() {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        return success(userId);
    }

    /**
     * * 获取经纬度度分秒
     *
     * @param jd
     * @param wd
     * @return
     */
    @GetMapping("/getDms")
    @ApiOperation("获取经纬度度分秒")
    public AjaxResult getDms(@RequestParam(value = "jd", required = false) String jd, @RequestParam(value = "wd", required = false) String wd) {
        Map hashMap = new HashMap();
        String[] split = null;
        String ll = "";
        if (StringUtils.isNotEmpty(jd)) {
            ll = LatLngConverter.convertDecimalToDegree(new BigDecimal(jd));
        } else {
            ll = LatLngConverter.convertDecimalToDegree(new BigDecimal(wd));
        }
        split = ll.split("°|'|''|\"");
        hashMap.put("degrees", split[0]);
        hashMap.put("minutes", split[1]);
        hashMap.put("seconds", split[2]);
        return success(hashMap);
    }

    /**
     * * 获取经纬度数值
     *
     * @param degrees
     * @param minutes
     * @param seconds
     * @return
     */
    @GetMapping("/getLatiAndLong")
    @ApiOperation("获取经纬度数值")
    public AjaxResult getLatiAndLong(@RequestParam("degrees") String degrees, @RequestParam("minutes") String minutes, @RequestParam String seconds) {
        String s = degrees + "°" + minutes + "'" + seconds + "''";
        BigDecimal bigDecimal = LatLngConverter.convertDegreeToDecimal(s);
        return success(bigDecimal);
    }

}
